package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.EmployeeMapper;
import org.javaboy.vhr.model.Employee;
import org.javaboy.vhr.model.PageBean;
import org.javaboy.vhr.responseBean.ServerResponse;
import org.javaboy.vhr.util.DateUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 根据部门id查询员工个数
     * @param id
     * @return
     */
    public int getEmployeeByDeptId(Integer id) {
        return employeeMapper.getEmployeeByDeptId(id);
    }

    /**
     * 分页查询员工数据
     * @param page
     * @param size
     * @return
     */
    public PageBean getEmployeeByPage(Integer page, Integer size,Employee employee,Date[] beginWorkDate) {

        long start = 0;
        if (page!=null && size!=null){
            start=(page-1) * size;
        }
        // 查询当前页的数据
        List<Employee> list = employeeMapper.getEmployeeByPage(start,size,employee,beginWorkDate);
        // 查询总条数
        Long total = employeeMapper.getEmployeeTotal(employee,beginWorkDate);
        return new PageBean(page,total,list);
    }

    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    public int addEmployee(Employee employee) {
        // 合同开始日期
        Date beginContract = employee.getBeginContract();
        // 合同截止日期
        Date endContract = employee.getEndContract();
        // 计算合同期限
        double workAge = DateUtil.getMonthTotal(beginContract, endContract);
        employee.setContractTerm(workAge);
        int result = employeeMapper.insertSelective(employee);
        if(result==1){
            // 重新查询 全部信息
            Employee emp = employeeMapper.getEmployeeById(employee.getId());
            // 发送入职欢迎邮件
            rabbitTemplate.convertAndSend("start-work-welcome",emp);
            System.out.println("发给rabbitmq成功");
        }
        return result;
    }

    /**
     * 获取最大工号
     * @return
     */
    public int getMaxWorkId() {
        return employeeMapper.getMaxWorkId();
    }

    /**
     * 删除员工信息
     * @param id
     * @return
     */
    public int deleteEmployeeById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    public int updateEmployee(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public ServerResponse getEmployeeAndSalary(Integer page, Integer size) {
        Integer start = null;
        if(page!=null && size !=null){
            start=(page-1)*size;
        }
        List<Employee> employees = employeeMapper.getEmployeeAndSalary(start,size);
        Long total = employeeMapper.getEmployeeTotal(new Employee(),null);

        return ServerResponse.success("",new PageBean(page,total,employees));
    }

    public int updateEmployeeSalary(Integer empId, Integer salaryId) {
        // 需要判断是新增还是修改
        // TODO 方案一
        // 根据empId查询有没有对应的值
        // 有修改
        // 无新增
        // TODO 方案二
        // 使用replace into () values ()
        // 条件是 以eid判断是增加还是修改 需要给eid字段添加唯一索引(UNIQUE KEY)
        return employeeMapper.updateEmployeeSalary(empId,salaryId);
    }
}
